#include "widget.h"
#include "./ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    text = new MyTextEdit(this);
    text->installEventFilter(this); // 安装事件过滤器，this???

}

Widget::~Widget()
{
    delete ui;
}

void Widget::mouseDoubleClickEvent(QMouseEvent *event)
{
    qDebug() << "双击";
}

void Widget::mousePressEvent(QMouseEvent *event)
{
    qDebug() << "鼠标按下";
}

void Widget::mouseReleaseEvent(QMouseEvent *event)
{
    qDebug() << "鼠标抬起";
}

void Widget::enterEvent(QEnterEvent *event)
{
    qDebug() << "移进窗口";
}

void Widget::leaveEvent(QEvent *event)
{
    qDebug() << "移出窗口";
}

void Widget::wheelEvent(QWheelEvent *event)
{
    qDebug() << "鼠标滚轮";
}

void Widget::keyPressEvent(QKeyEvent *event)
{
    switch (event->key()) {
    case Qt::Key_A:
        qDebug() << "按下 A";
        break;
    case Qt::Key_W:
        qDebug() << "按下 W";
        break;
    case Qt::Key_S:
        qDebug() << "按下 S";
        break;
    case Qt::Key_D:
        qDebug() << "按下 D";
        break;
    default:
        break;
    }
}

void Widget::closeEvent(QCloseEvent *event)
{
    int ret = QMessageBox::question(this, QString("关闭"),
                                    QString("确定要退出吗？"), QMessageBox::Yes, QMessageBox::No);

    if (QMessageBox::Yes == ret) {
        event->accept();
    } else {
        event->ignore();
    }

}

bool Widget::eventFilter(QObject *watched, QEvent *event)
{

    if (event->type() == QEvent::Wheel) {
        qDebug() << "filter";

        if (!(QGuiApplication::keyboardModifiers() && Qt::ControlModifier))
            return QWidget::eventFilter(watched, event);

        QWheelEvent *e = dynamic_cast<QWheelEvent *>(event);
        if (e->angleDelta().y() > 0)
            text->rollIn();
        else
            text->rollOut();

        return true;
    }

    return QWidget::eventFilter(watched, event);
}
